package cn.chi.user.service.impl;

import cn.chi.base.service.impl.BaseServiceImpl;
import cn.chi.org.domain.Employee;
import cn.chi.org.mapper.EmployeeMapper;
import cn.chi.user.domain.LoginInfo;
import cn.chi.user.domain.User;
import cn.chi.user.mapper.LoginInfoMapper;
import cn.chi.user.service.ILogininfoService;
import cn.chi.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LogininfoServiceImpl extends BaseServiceImpl<LoginInfo> implements ILogininfoService {

    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public LoginInfo login(User user) {
        // 查询用户
        LoginInfo loginInfo =loginInfoMapper.loadByCount(user.getPhone());
        if(loginInfo==null){
            // 有问题
            throw new RuntimeException("用户不存在请注册");
        }
        // 验证密码是否正确
        //把用户输入的明文进行加盐 加密
        String byMd5 = MD5Utils.encrypByMd5(user.getPassword() + loginInfo.getSalt());
        if(!byMd5.equals(loginInfo.getPassword())){
            throw new RuntimeException("账户或密码错误");
        }

        return loginInfo;
    }

    @Override
    public LoginInfo loginemp(Employee employee) {
        LoginInfo loginInfo = loginInfoMapper.loadByCount(employee.getUsername());
        System.out.println(employee.getUsername());
        if (loginInfo==null){
            throw new RuntimeException("用户未注册");
        }
        //判断密码是否正确
        //把用户输入的明文进行加盐 加密
        String byMd5 = MD5Utils.encrypByMd5(employee.getPassword() + loginInfo.getSalt());
        if (!byMd5.equals(loginInfo.getPassword())){
            throw new RuntimeException("密码或用户名不正确");
        }
        Employee employeeDB = employeeMapper.loadByUserName(employee.getUsername());
        if(employeeDB.getState()!=1){
            throw new RuntimeException("你的账号被封啦！");
        }
        return loginInfo;
    }

}
